/*
 * @Descripttion: 
 * @Author: yuxi
 * @Date: 2024-03-04 20:25:58
 * @LastEditors: yuxi
 * @LastEditTime: 2024-03-04 22:20:08
 */ 
function getUrlQueryParams(url = location.search){
  const params = {};
  const keys = url.match(/([^?&]+)(?==)/g);
  const values = url.match(/(?<==)([^&]*)/g);
  for(const index in keys){
  params[keys[index]] = values[index];
  }
  return params;
}
let {uid} = getUrlQueryParams() 
chrome.runtime.sendMessage(
  {
    action: "getLouguResult",
    uid: uid
  },
  (res) => {
    console.log(res)
  }
); 

var TableDatas  = [];
function getCSV(){
    if(TableDatas.length === 0){
        alert("没有数据呀呀呀呀！");
        return false;
    }
    const jsonData = {
        target: {
          header: ['是否有效', '区分', 'URL', '担当', '日期', '説明'],
          filter: ['url_flag', 'division', 'url_value', 'opt_user_nm', 'opt_datetime', 'url_note'],
          list: TableDatas
        }
    };
    exportMethod(jsonData);
}
function exportMethod(data) {
    const csvTitle = data.target.header;
    const csvFilter = data.target.filter;
    const csvList = data.target.list;
    const csvData = [];
    csvData.push('"' + csvTitle.join('","') + '"\n');
    for (let i = 0; i < csvList.length; i++) {
        const tmp = [];
　　　　　for (let j = 0; j < csvFilter.length; j++) {
        　　var d = csvList[i][csvFilter[j]];
        　　tmp.push(d);
    　　}
    　　csvData.push('"' + tmp.join('","') + '"\n');
     }
     // csv download
     const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(csvData.join(''));
     const link = document.createElement('a');
     link.href = uri;
     link.download = `text.csv`;
     document.body.appendChild(link);
     link.click();
}
getCSV()